//
//  FrozenSwitchNumberViewController.swift
//  AlterationSwitch
//
//  Created by solarke_apple_mini_1 on 2018/10/25.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit

class FrozenSwitchNumberViewController: BaseViewController {
    // MARK: - Properties
    var typeIndex = 0
    var timeChoose = ""
    var year:Int!
    var month:Int!
    var day:Int!
    // MARK: - IBOutlet
    @IBOutlet weak var measureDotTextField: UITextField!
    @IBOutlet weak var timeButton:UIButton!
    @IBOutlet weak var backGroundView:UIView!
    @IBOutlet weak var timeDatePicker:UIDatePicker!
    @IBOutlet weak var timeResultLabel: UILabel!
    @IBOutlet weak var swichToltolTimesLablel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        measureDotTextField.text = "1"
        backGroundView.isHidden = true
        timeDatePicker.isHidden = true
        timeDatePicker.setDate(Date(), animated: true)
        timeDatePicker.maximumDate = Date()
        notificationCenter.addObserver(self, selector: #selector(dealWithFrozenSwitchNumber(sender:)), name: DayFrozenSwitchNumberNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(dealWithFrozenSwitchNumber(sender:)), name: MonthFrozenSwitchNumberNotification, object: nil)
    }
    
    override func viewControllerWillPresent(_ data: AnyObject) {
        typeIndex = data["index"] as! Int
        self.navigationItem.title = leiShuju[typeIndex]
        getTime()
    }
    
    @objc func dealWithFrozenSwitchNumber(sender: NSNotification) {
        let info = sender.userInfo as! [String: AnyObject]
        let data = info["data"] as AnyObject
        let time = data.object(forKey: "time") as! String
        timeResultLabel.text = time
        let times = data.object(forKey: "times") as! Int
        swichToltolTimesLablel.text = String(times)
    }
    
    func dayQuery() {
        if viery() {
            let time = timeChoose.components(separatedBy: "-")
            let year:Int32 = Int32(time[0])!
            let month:UInt8 = UInt8(time[1])!
            let day:UInt8 = UInt8(time[2])!
            let measureDot = measureDotTextField.text
            let mpNo:Int32 = Int32(measureDot!)!
            let termaddr:UnsafeMutablePointer<UInt8> = getTermaddr()
            let askTwoData_fn239_send = ocFile().askTwoData_fn239_send(termaddr, a3: a3, mpNo: mpNo, year: year, month: month, day: day)
            var arr = [UInt8](repeating: 0, count: 37)
            //将dst中的所有元素拷贝到arr中
            for i in 0..<37 {
                arr[i] = UInt8(askTwoData_fn239_send[i])
            }
            sendMessageBytes(msg: arr)
        }
    }
    
    func monthQuery() {
        if viery() {
            let time = timeChoose.components(separatedBy: "-")
            let year:Int32 = Int32(time[0])!
            let month:UInt8 = UInt8(time[1])!
            let measureDot = measureDotTextField.text
            let mpNo:Int32 = Int32(measureDot!)!
            let termaddr:UnsafeMutablePointer<UInt8> = getTermaddr()
            let askTwoData_fn240_send = ocFile().askTwoData_fn240_send(termaddr, a3: a3, mpNo: mpNo, year: year, month: month)
            var arr = [UInt8](repeating: 0, count: 37)
            //将dst中的所有元素拷贝到arr中
            for i in 0..<37 {
                arr[i] = UInt8(askTwoData_fn240_send[i])
            }
            sendMessageBytes(msg: arr)
        }
    }
    
    func viery() -> Bool {
        let measureDot = measureDotTextField.text
        if (measureDot?.isEmpty)! {
            showToast("请输入测量点号!")
            return false
        }
        return true
    }
    
    func getTime() {
        //获取当前时间
        let now = Date()
        // 创建一个日期格式器
        let dformatter = DateFormatter()
        if typeIndex == 0 {
            dformatter.dateFormat = "yyyy-MM-dd"
        } else {
            dformatter.dateFormat = "yyyy-MM"
        }
        timeChoose = dformatter.string(from: now)
        timeButton.setTitle(timeChoose, for: .normal)
    }
    
    @IBAction func queryButtonTap() {
        if typeIndex == 0 {
            dayQuery()
        } else {
            monthQuery()
        }
        
    }
    
    @IBAction func timeButtonTap() {
        backGroundView.isHidden = false
        timeDatePicker.isHidden = false
    }
    
    @IBAction func timeDateChange(datePicker:UIDatePicker) {
        let dformatter = DateFormatter()
        if typeIndex == 0 {
            dformatter.dateFormat = "yyyy-MM-dd"
        } else {
            dformatter.dateFormat = "yyyy-MM"
        }
        let timeChoose = dformatter.string(from: datePicker.date)
        timeButton.setTitle(timeChoose, for: .normal)
    }
    
    @IBAction func backViewTap() {
        backGroundView.isHidden = true
        timeDatePicker.isHidden = true
    }
    
    @IBAction func touchDown() {
        view.endEditing(true)
    }
}
